package com.kinghood.productcenter.manager.impl;

import com.kinghood.infrastructure.model.KingHoodExceptionUtil;
import com.kinghood.productcenter.common.model.dao.product.ProductAttributeDO;
import com.kinghood.productcenter.common.model.dao.product.ProductBaseDO;
import com.kinghood.productcenter.common.model.dto.common.IdReqDTO;
import com.kinghood.productcenter.common.model.dto.product.BaseProductDTODO;
import com.kinghood.productcenter.common.model.dto.product.ProductDTO;
import com.kinghood.productcenter.common.model.dto.restcloud.GetProductInfoReqDTO;
import com.kinghood.productcenter.common.properties.BaseProperties;
import com.kinghood.productcenter.common.utils.WebClientRpc;
import com.kinghood.productcenter.manager.IRestCloudManager;
import com.kinghood.productcenter.service.product.IProductAttributeService;
import com.kinghood.productcenter.service.product.IProductBaseService;
import com.kinghood.productcenter.service.product.handler.ProductBaseHandler;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.*;
import java.util.stream.Collectors;

/**
 * description
 *
 * @author zhangFanJun
 * @date 2024-01-29 10:54
 **/
@RequiredArgsConstructor
@Slf4j
@Service
public class RestCloudManager implements IRestCloudManager {

    private final IProductBaseService productBaseService;
    private final IProductAttributeService productAttributeService;
    private final WebClientRpc webClientRpc;
    private final BaseProperties baseProperties;

    @Override
    public void pushProduct(IdReqDTO in) {
        ProductDTO product = productBaseService.getProduct(in.getId());
        KingHoodExceptionUtil.isNull(product, "需要推送的商品不存在");

        webClientRpc.restCloud().post(baseProperties.getRestCloudProductUri(), product);

    }

    @Override
    public List<ProductDTO> getProductInfo(GetProductInfoReqDTO in) {

        List<ProductDTO> list = new ArrayList<>();
        if(Objects.nonNull(in.getId())){
            // 临时用于测试单个同步，具体同步按照需求开发
            ProductDTO product = productBaseService.getProduct(in.getId());
            list.add(product);
        }

        List<BaseProductDTODO> productList = productBaseService.getProductInfo(in);
        if(CollectionUtils.isEmpty(productList)){
            return null;
        }
        productList.forEach(BaseProductDTODO::handNull);

        List<Long> productIdList = productList.stream()
                .map(ProductBaseDO::getId)
                .filter(Objects::nonNull).collect(Collectors.toList());
        List<ProductAttributeDO> attributeList = productAttributeService.getProductInfo(productIdList);
        Map<String, List<ProductAttributeDO>> attributeMap = attributeList.stream()
                .collect(Collectors.groupingBy(k ->  k.getBusinessId() + "-" + k.getBusinessType()));

        for (BaseProductDTODO product : productList) {
            ProductDTO res = new ProductDTO();
            BeanUtils.copyProperties(product, res);

            res.setProductBase(ProductBaseHandler.getProductBase(product, attributeMap));
            res.setSkuList(ProductBaseHandler.getSkuList(product, attributeMap));
            res.setSkuExtrasList(ProductBaseHandler.getSkuExtrasList(product, attributeMap));
            res.setSkuSupplierList(ProductBaseHandler.getSkuSupplierList(product, attributeMap));
            list.add(res);
        }

        return list;
    }
}
